/*
 * CapsLock Notification based on pipe-notification by Armin Preiml <b.nutzer@gmail.com>
 *
 * Copyright (C) 2009  Przemek Aksamit <extaliones@gmail.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * set ts=4
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#ifndef PURPLE_PLUGINS
# define PURPLE_PLUGINS
#endif

#include <glib.h>
#include <string.h>

#include "notify.h"
#include "plugin.h"
#include "pluginpref.h"
#include "prefs.h"
#include "version.h"
#include "status.h"
#include "savedstatuses.h"
#include "signals.h"
#include <pthread.h>

#include "gtkconv.h"

int keep_blinking = TRUE;
struct timespec delay;
pthread_t thread;

void *blink(void *threadid)
{
  int pref_delay = purple_prefs_get_int("/plugins/gtk/capslock-notification/blink_delay");
  delay.tv_nsec = pref_delay * 1000000;
  while(keep_blinking){
		system("xset led 3");
    nanosleep(&delay, NULL);
    system("xset -led 3");
    nanosleep(&delay, NULL);
  }
  pthread_exit(NULL);
}

GList *get_pending_list(guint max) {

	const char *im=purple_prefs_get_string("/plugins/gtk/capslock-notification/im");
	const char *chat=purple_prefs_get_string("/plugins/gtk/capslock-notification/chat");

	GList *l_im = NULL;
	GList *l_chat = NULL;

	if (im != NULL && strcmp(im, "always") == 0) {
		l_im = pidgin_conversations_find_unseen_list(PURPLE_CONV_TYPE_IM,
		                                             PIDGIN_UNSEEN_TEXT,
		                                             FALSE, max);
	} else if (im != NULL && strcmp(im, "hidden") == 0) {
		l_im = pidgin_conversations_find_unseen_list(PURPLE_CONV_TYPE_IM,
		                                             PIDGIN_UNSEEN_TEXT,
		                                             TRUE, max);
	}

	if (chat != NULL && strcmp(chat, "always") == 0) {
		l_chat = pidgin_conversations_find_unseen_list(PURPLE_CONV_TYPE_CHAT,
		                                               PIDGIN_UNSEEN_TEXT,
		                                               FALSE, max);
	} else if (chat != NULL && strcmp(chat, "nick") == 0) {
		l_chat = pidgin_conversations_find_unseen_list(PURPLE_CONV_TYPE_CHAT,
		                                               PIDGIN_UNSEEN_NICK,
		                                               FALSE, max);
	}

	if (l_im != NULL && l_chat != NULL)
		return g_list_concat(l_im, l_chat);
	else if (l_im != NULL)
		return l_im;
	else 
		return l_chat;
}

static void conversation_updated(PurpleConversation *conv, 
                                        PurpleConvUpdateType type) {
	GList *list;

	if( type != PURPLE_CONV_UPDATE_UNSEEN ) {
		return;
	}

	list = get_pending_list(1);

	if(list!=NULL) {
    if(keep_blinking == FALSE){
      keep_blinking = TRUE;
      pthread_create(&thread, NULL, blink, NULL);
    }
	} else {
    keep_blinking = FALSE;
	}
	g_list_free(list);
}

static PurplePluginPrefFrame *
get_plugin_pref_frame(PurplePlugin *plugin) {
        PurplePluginPrefFrame *frame;
   		PurplePluginPref *ppref;

    frame = purple_plugin_pref_frame_new();

    ppref = purple_plugin_pref_new_with_name_and_label(
                                                        "/plugins/gtk/capslock-notification/blink_delay",
                                                        "Blink delay");
        purple_plugin_pref_set_bounds(ppref, 100, 1000);
        purple_plugin_pref_frame_add(frame, ppref);

		ppref = purple_plugin_pref_new_with_name_and_label(
                                                        "/plugins/gtk/capslock-notification/im",
                                                        "Instant Messages");
        purple_plugin_pref_set_type(ppref, PURPLE_PLUGIN_PREF_CHOICE);
        purple_plugin_pref_add_choice(ppref, "Never", "never");
        purple_plugin_pref_add_choice(ppref, "In hidden conversations", "hidden");
        purple_plugin_pref_add_choice(ppref, "Always", "always");
        purple_plugin_pref_frame_add(frame, ppref);

		ppref = purple_plugin_pref_new_with_name_and_label(
                                                        "/plugins/gtk/capslock-notification/chat",
                                                        "Chat Messages");
        purple_plugin_pref_set_type(ppref, PURPLE_PLUGIN_PREF_CHOICE);
        purple_plugin_pref_add_choice(ppref, "Never", "never");
        purple_plugin_pref_add_choice(ppref, "When my nick is said", "nick");
        purple_plugin_pref_add_choice(ppref, "Always", "always");
        purple_plugin_pref_frame_add(frame, ppref);

		return frame;
}


static gboolean plugin_load(PurplePlugin *plugin) {
	purple_signal_connect(purple_conversations_get_handle(),
	                      "conversation-updated", plugin,
	                      PURPLE_CALLBACK(conversation_updated), NULL);
	return TRUE;
}

static gboolean plugin_unload(PurplePlugin *plugin) {
	purple_signal_disconnect(purple_conversations_get_handle(),
	                         "conversation-updated", plugin,
	                         PURPLE_CALLBACK(conversation_updated));
  keep_blinking = FALSE;
  return TRUE;
}

void plugin_destroy(PurplePlugin *plugin) {
  keep_blinking = FALSE;
}

static PurplePluginUiInfo prefs_info = {
        get_plugin_pref_frame,
        0,   /* page_num (Reserved) */
        NULL, /* frame (Reserved) */
        /* Padding */
        NULL,
        NULL,
        NULL,
        NULL
};

static PurplePluginInfo info = {
    PURPLE_PLUGIN_MAGIC,
    PURPLE_MAJOR_VERSION,
    PURPLE_MINOR_VERSION,
    PURPLE_PLUGIN_STANDARD,
    NULL,
    0,
    NULL,
    PURPLE_PRIORITY_DEFAULT,

    "capslock_notification",
    "CapsLock Notification",
    "0.1",

    "Notify about new messages by blinking with CapsLock led",          
    "Notify about new messages by blinking with CapsLock led",          
    "extaliones <extaliones@gmail.com>",                          
    "http://code.google.com/p/capslock-notification/",     
    plugin_load,                   
    plugin_unload,                          
    plugin_destroy,                          
    NULL,
    NULL,
    &prefs_info,                          
    NULL,                   
    NULL,                          
    NULL,                          
    NULL,                          
    NULL                           
};                               
    
static void                        
init_plugin(PurplePlugin *plugin)
{                                  
  delay.tv_sec = 0;
	purple_prefs_add_none("/plugins/gtk/capslock-notification");
	purple_prefs_add_int("/plugins/gtk/capslock-notification/blink_delay", 150);
	purple_prefs_add_string("/plugins/gtk/capslock-notification/im", "always");
	purple_prefs_add_string("/plugins/gtk/capslock-notification/chat", "nick");
}

PURPLE_INIT_PLUGIN(hello_world, init_plugin, info)

